﻿// ******************************************************************************************************
// <copyright file="MyPlugin.cs" company="Caroso.inc">
//     Copyright (c) Carlos Osorio All rights reserved.
// </copyright>
// ******************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http:// www.mozilla.org/MPL/ 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either express or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
// 
// The Original Code is this . mapWindow 
// 
// The Initial Developer of this version of the Original Code is Carlos Osorio carosocali@gmail.com
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
// Change Log: 
// Date           Changed By      Notes
// 22 Nov 2010    Carlos Osorio   Inital upload 
// 17 Abr 2011    Carlos Osorio   
// ******************************************************************************************************

namespace MapWinPlugin
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using MapWindow.Interfaces;
    using MapWinGeoProc;

    /// <summary>
    /// This is the start of the plug-in. In here all methods of the IPlugin interface are implemented.
    /// </summary>
    public class MyPlugin : IPlugin
    {
        /// <summary>
        /// Name of the menu
        /// </summary>
        private const string MenuHeading = "Interpolation Methods";

        /// <summary>
        /// Name of the subMenu
        /// </summary>
        private const string SubMenu1 = "Kriging";

        /// <summary>
        /// Global variable to manage the environment of Mapwindo
        /// </summary>
        private IMapWin myMapWin;

        /// <summary>
        /// Gets the author of the plugin.
        /// </summary>
        public string Author
        {
            get { return "Carlos Andres Osorio M"; }
        }

        /// <summary>
        /// Gets build date when compiled the plugin.
        /// </summary>
        public string BuildDate
        {
            get { return System.DateTime.Now.ToString(); }
        }

        /// <summary>
        /// Gets description of the plugin
        /// </summary>
        public string Description
        {
            get { return "This plugin implements the method of interpolation Ordinary Kriging"; }
        }

        /// <summary>
        /// Gets Serial number
        /// </summary>
        public string SerialNumber
        {
            get { return null; }
        }

        /// <summary>
        /// Gets the name of the plugin
        /// </summary>
        public string Name
        {
            get { return "Kriging"; }
        }

        /// <summary>
        /// Gets the version of the plugin
        /// </summary>
        public string Version
        {
            get { return "1.0"; }
        }

        /// <summary>
        /// Creation of principal Menu
        /// </summary>
        /// <param name="mapWin">Global cariable</param>
        /// <param name="parentHandle">Id parentHandel</param>
        public void Initialize(IMapWin mapWin, int parentHandle)
        {
            this.myMapWin = mapWin;
            this.myMapWin.Menus.AddMenu(MenuHeading);
            this.myMapWin.Menus.AddMenu(SubMenu1, MenuHeading).Enabled = true;
            this.myMapWin.Menus.AddMenu("Info", MenuHeading).Enabled = true;
            this.EvaluationTypeLayer(SubMenu1);
       }

        /// <summary>
        /// Evaluate if there are  any point shapefile opens
        /// </summary>
        /// <param name="menuName">menu evaluated</param>
        public void EvaluationTypeLayer(string menuName)
        {
            this.myMapWin.Menus[menuName].Enabled = true;
            /*for (int i = 0; i < this.myMapWin.Layers.NumLayers; i++)
            {
                if (this.myMapWin.Layers[i].LayerType == eLayerType.PointShapefile)
                {
                    this.myMapWin.Menus[menuName].Enabled = true;
                }
            }*/
        }

        /// <summary>
        /// This method joins with the form called Select
        /// </summary>
        /// <param name="itemName">Get the item clicked</param>
        /// <param name="handled">Handled the item</param>
        public void ItemClicked(string itemName, ref bool handled)
        {
            switch (itemName)
            {
                case SubMenu1:
                    handled = true;
                    Select myForm = new Select(this.myMapWin);
                    myForm.Show();
                    ////Test();
                    break;
                case "Info":
                    MessageBox.Show(@"Kriging Plug-in starts only when there is a projected point shapefile in the view.", "Info");
                    break;
            }
        }

        /// <summary>
        /// When a layer is removed this method evaluates that yet exist a point shapefile
        /// </summary>
        /// <param name="handle">The handle of the group or layer.</param>
        public void LayerRemoved(int handle)
        {
            this.EvaluationTypeLayer(SubMenu1);
        }

        /// <summary>
        /// This method is executed when an layer is seleted
        /// </summary>
        /// <param name="handle">The handle of the group or layer.</param>
        public void LayerSelected(int handle)
        {
        }

        /// <summary>
        /// This method is executed when a layer is added
        /// </summary>
        /// <param name="layers">List of layers</param>
        public void LayersAdded(Layer[] layers)
        {
            this.EvaluationTypeLayer(SubMenu1);
        }

        /// <summary>
        /// This method is executed when it is cleared an layer
        /// </summary>
        public void LayersCleared()
        {
            this.EvaluationTypeLayer(SubMenu1);
        }

        /// <summary>
        /// This method execute an action when there is an double click in the legend
        /// </summary>
        /// <param name="handle">The handle of the legend group or item that was clicked on.</param>
        /// <param name="location">Enumerated.  The location clicked on.</param>
        /// <param name="handled">Reference parameter.  When set to true it prevents additional plugins from getting this event.</param>
        public void LegendDoubleClick(int handle, ClickLocation location, ref bool handled)
        {
        }

        /// <summary>
        /// Occurs when a user presses a mouse button on the legend.
        /// </summary>
        /// <param name="handle">Layer or group handle that was clicked.</param>
        /// <param name="button">The integer representation of the button used.  Uses vb6 mouse button constants.</param>
        /// <param name="location">The part of the legend that was clicked.</param>
        /// <param name="handled">Reference parameter.  Prevents other plugins from getting this event when set to true.</param>
        public void LegendMouseDown(int handle, int button, ClickLocation location, ref bool handled)
        {
        }

        /// <summary>
        /// Occurs when the user releases a mouse button over the legend.
        /// </summary>
        /// <param name="handle">The handle of the group or layer.</param>
        /// <param name="button">The integer representation of the button released.  Uses vb6 button constants.</param>
        /// <param name="location">Enumeration.  Specifies if a group, layer or neither was clicked on.</param>
        /// <param name="handled">Reference parameter.  Prevents other plugins from getting this event.</param>
        public void LegendMouseUp(int handle, int button, ClickLocation location, ref bool handled)
        {
        }

        /// <summary>
        /// Occurs after a user selects a rectangular area in the MapWindow.  Normally this implies 
        /// selection.
        /// </summary>
        /// <param name="bounds">The rectangle selected.</param>
        /// <param name="handled">Reference parameter.  Setting Handled to true prevents other plugins from 
        /// receiving this event.</param>
        public void MapDragFinished(System.Drawing.Rectangle bounds, ref bool handled)
        {
        }
        
        /// <summary>
        /// Occurs after change the extents
        /// </summary>
        public void MapExtentsChanged()
        {
        }

        /// <summary>
        /// Occurs when a user releases a mouse button on the this.mapWindow main map display.
        /// </summary>
        /// <param name="button">An integer representation of which button(s) were released.  Uses vb6 button constants.</param>
        /// <param name="shift">An integer representation of the shift/alt/ctrl keys that were pressed at the time the mouse button was released.  Uses vb6 shift constants.</param>
        /// <param name="x">X coordinate in pixels.</param>
        /// <param name="y">Y coordinate in pixels.</param>
        /// <param name="handled">Reference parameter.  Prevents other plugins from getting this event.</param>
        public void MapMouseDown(int button, int shift, int x, int y, ref bool handled)
        {
        }

        /// <summary>
        /// Occurs when a user moves the mouse over the this.mapWindow main display.
        /// </summary>
        /// <param name="screenX">X coordinate in pixels.</param>
        /// <param name="screenY">Y coordinate in pixels.</param>
        /// <param name="handled">Reference parameter.  Prevents other plugins from getting this event.</param>
        public void MapMouseMove(int screenX, int screenY, ref bool handled)
        {
        }

        /// <summary>
        /// Occurs when a user releases a mouse button on the this.mapWindow main map display.
        /// </summary>
        /// <param name="button">An integer representation of which button(s) were released.  Uses vb6 button constants.</param>
        /// <param name="shift">An integer representation of the shift/alt/ctrl keys that were pressed at the time the mouse button was released.  Uses vb6 shift constants.</param>
        /// <param name="x">X coordinate in pixels.</param>
        /// <param name="y">Y coordinate in pixels.</param>
        /// <param name="handled">Reference parameter.  Prevents other plugins from getting this event.</param>
        public void MapMouseUp(int button, int shift, int x, int y, ref bool handled)
        {
        }

        /// <summary>
        /// Display a message
        /// </summary>
        /// <param name="msg">Message to display</param>
        /// <param name="handled">Reference parameter.  Prevents other plugins from getting this event.</param>
        public void Message(string msg, ref bool handled)
        {
        }

        /// <summary>
        /// Occurs when a project is loading
        /// </summary>
        /// <param name="projectFile">Source of the project file</param>
        /// <param name="settingsString">Settings of the project</param>
        public void ProjectLoading(string projectFile, string settingsString)
        {
        }

        /// <summary>
        /// Occurs when a project is saving
        /// </summary>
        /// <param name="projectFile">Source of the project file</param>
        /// <param name="settingsString">Settings of the project</param>
        public void ProjectSaving(string projectFile, ref string settingsString)
        {
        }

        /// <summary>
        /// Occurs when a shape is selected
        /// </summary>
        /// <param name="handle">The handle of the group or layer.</param>
        /// <param name="selectInfo">Information of the selection</param>
        public void ShapesSelected(int handle, SelectInfo selectInfo)
        {
        }

        /// <summary>
        /// Occurs when terminate the application 
        /// </summary>
        public void Terminate()
        {
            this.myMapWin.Menus.Remove(MenuHeading);
            this.myMapWin.Menus.Remove(SubMenu1);
        }

        /// <summary>
        /// This method execute a test of the Kriging plugin ( it´s needed a shapefile)
        /// It its needed to desable the menus
        /// </summary>
        public void Test() 
        {
            ModelExponential model = new ModelExponential();
            model.Range = 700;
            model.C0 = 0;
            model.C1 = 300;
            MapWinGIS.Shapefile shapeLayer = new MapWinGIS.Shapefile();
            shapeLayer.Open("C:/temp/puntos.shp", null);
            Kriging mkriging;
            mkriging = new Kriging(shapeLayer, 5, model);

            double height = Math.Abs(shapeLayer.Extents.yMax - shapeLayer.Extents.yMin);
            double width = Math.Abs(shapeLayer.Extents.xMax - shapeLayer.Extents.xMin);
            double defaultCellSize = -1;
            if (height <= width)
            {
                defaultCellSize = height / 50;
            }
            else
            {
                defaultCellSize = width / 50;
            }

            string salida = "C:/temp/salida.img";
            MessageBox.Show("Cellsize:" + defaultCellSize.ToString() + " Projection: " + shapeLayer.Projection);
            mkriging.Surface(shapeLayer.Extents, defaultCellSize, shapeLayer.Projection, -32768, salida, 5);
            MessageBox.Show("Ok..");
        }
    }
}
